<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>友情链接列表</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          @click="dialogFormVisible = true"
        >添加友情链接</el-button>
        <el-table :data="tableData" style="width: 100%" size="medium">
          <el-table-column prop="key" label="ID" width="100"></el-table-column>
          <el-table-column prop="sitename" label="网站名称" width="300"></el-table-column>

          <el-table-column prop="siteurl" label="网站地址" width="300"></el-table-column>

          <el-table-column prop="audit" label="审核状态" width="300">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.audit"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ccc"
                @change="changeAudit($event, scope.row.id)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <!-- 点击编辑按钮，需要把当前这条信息的id传给跳转的那个页面 -->
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                circle
                @click="onEdit(scope.row)"
              ></el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="page">
          <!-- layout, 显示的内容：prev 上一页； next 下一页； pager 页码列表； sizes 每页多少条， jumper 跳转第n页-->
          <el-pagination
            background
            layout="total, prev, pager, next, sizes, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 30, 50]"
          ></el-pagination>
        </div>

        <!-- 对话框 -->
        <el-dialog
          title="友情链接"
          :visible.sync="dialogFormVisible"
          :modal-append-to-body="false"
          width="25%"
          :close-on-click-modal="false"
        >
          <el-form :model="formData" :rules="myrules" ref="myform">
            <el-form-item prop="sitename">
              <el-input v-model="formData.sitename" autocomplete="off" placeholder="网站名称"></el-input>
            </el-form-item>
            <el-form-item prop="siteurl">
              <el-input v-model="formData.siteurl" autocomplete="off" placeholder="网站地址"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="onSave('myform')">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "@/api/flink";
import "@/utils/filters.js";
export default {
  data() {
    return {
      tableData: [],
      curr: 1, // 当前第几页
      limit: 10, // 每页多少条
      total: 100, // 一共多少条
      dialogFormVisible: false, // 控制对话框显示or隐藏
      formData: {
        sitename: "",
        siteurl: "",
      },
      myrules: {
        sitename: [
          { required: true, message: "网站名称不能为空", trigger: "blur" },
        ],
        siteurl: [
          { required: true, message: "网站地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getlist();
  },
  methods: {
    // ajax请求获取口碑列表
    getlist() {
      request.getFlinkList({ curr: this.curr, limit: this.limit }).then((r) => {
        // console.log(r.data.list);
        this.total = r.data.total;
        // 遍历r.data.list数组， 给每一项添加一个 key, 第一页key(1,2,3...10); 第2页key(11,12,13,...20)
        r.data.list.forEach((item, index) => {
          item.key = index + 1 + (this.curr - 1) * this.limit;
        });
        this.tableData = r.data.list;
      });
    },

    // 事件：改变当前第几页
    handleCurrentChange(val) {
      this.curr = val;
      this.getlist();
    },

    // 事件：改变每页多少条
    handleSizeChange(val) {
      this.limit = val;
      this.getlist();
    },

    del(myid) {
      // 确认框
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除的ajax操作
          request.delFlink({ id: myid }).then((r) => {
            if (r.data.code === 0) {
              this.$message({
                type: "success",
                message: "删除成功！",
              });
              this.getlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 添加-保存事件
    onSave(formname) {
      this.$refs[formname].validate((valid) => {
        if (valid) {
          request.addFlink(this.formData).then((r) => {
            console.log(r.data);
            if (r.data.code === 0) {
              this.$message({
                type: "success",
                message: "添加成功！",
              });

              this.getlist();
              // 关闭弹框
              this.dialogFormVisible = false;
              // 添加成功后重置表单
              this.$refs[formname].resetFields();
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    onEdit(row) {
      // 开启弹框
      this.dialogFormVisible = true;
      this.formData = row
    },

    // 审核状态改变事件
    changeAudit(val, id) {
      console.log(val, id);
      request.changeFlinkAudit({ id: id, audit: val }).then((r) => {
        if (r.data.code === 0) {
          this.$message({
            type: "success",
            message: "审核状态已改变！",
          });
        }
      });
    },
  },
};
</script>

<style>
.page {
  margin-top: 15px;
}
</style>